***ASSET VERIFICATION***

foreach n in 02 04 06 08 10 12 14 {
use "${original}H`n'U_H.dta", clear
rename *,lower
save assetverif_`n'.dta, replace
}

*2002
use assetverif_02.dta, clear
drop gsubhh
ren ?subhh subhh
ren ?u#* .u#*
ren ?pn_cs pn

g str asst_type_verif = ""
replace asst_type_verif = "debts" if typasst == 1
replace asst_type_verif = "trusts" if typasst == 2
replace asst_type_verif = "oth_asst" if typasst == 3
replace asst_type_verif = "transport" if typasst == 4
replace asst_type_verif = "cod" if typasst == 5
replace asst_type_verif = "svng_chck_acc" if typasst == 6
replace asst_type_verif = "bond" if typasst == 7
replace asst_type_verif = "stck" if typasst == 8
replace asst_type_verif = "ira" if typasst == 9
replace asst_type_verif = "bsns" if typasst == 10
replace asst_type_verif = "real_est" if typasst == 11
replace asst_type_verif = "prim_res" if typasst == 12
replace asst_type_verif = "mrtg_prres" if typasst == 13
replace asst_type_verif = "mrtg2_prres" if typasst == 14
replace asst_type_verif = "eqln_prres" if typasst == 15
replace asst_type_verif = "mob_home" if typasst == 16
replace asst_type_verif = "scnd_home" if typasst == 17
replace asst_type_verif = "mrtg_scnd_home" if typasst == 18

g wrongrecord_ = 1 if u001 == 5
replace wrongrecord_ = 0 if u001 == 1

drop typasst hqnr u* ?pn* ?version
duplicates drop
reshape wide wrong* , i(hhid subhh) j(asst_type_verif) string

lab def yesno 1 "yes" 0 "no"
lab val wrongrecord* yesno
g year = 2002
save asstvrf02.dta, replace


*2004
use assetverif_04.dta, clear
drop hsubhh
ren ?subhh subhh
ren ?u#* .u#*
ren ?pn_cs pn

unab vars: u*_1
local stubs: subinstr local vars "_1" "",all
reshape long `stubs',i(hhid pn subhh) j(n) string
drop if u001 == .
unab vars: u*a
local stubs_a: subinstr local vars "a" "",all
local stubs: subinstr local stubs_a "u022" "",all
reshape long `stubs',i(hhid pn subhh u009 n) j(n2) string
drop n*
duplicates drop
drop if u001 == .
clonevar typasst = u009 
g str asst_type_verif = ""
replace asst_type_verif = "debts" if typasst == 1
replace asst_type_verif = "trusts" if typasst == 2
replace asst_type_verif = "oth_asst" if typasst == 3
replace asst_type_verif = "transport" if typasst == 4
replace asst_type_verif = "cod" if typasst == 5
replace asst_type_verif = "svng_chck_acc" if typasst == 6
replace asst_type_verif = "bond" if typasst == 7
replace asst_type_verif = "stck" if typasst == 8
replace asst_type_verif = "ira" if typasst == 9
replace asst_type_verif = "bsns" if typasst == 10
replace asst_type_verif = "real_est" if typasst == 11
replace asst_type_verif = "prim_res" if typasst == 12
replace asst_type_verif = "mrtg_prres" if typasst == 13
replace asst_type_verif = "mrtg2_prres" if typasst == 14
replace asst_type_verif = "eqln_prres" if typasst == 15
replace asst_type_verif = "mob_home" if typasst == 16
replace asst_type_verif = "scnd_home" if typasst == 17
replace asst_type_verif = "mrtg_scnd_home" if typasst == 18

g wrongrecord_ = 1 if u001 == 5
replace wrongrecord_ = 0 if u001 == 1

drop typasst u* ?pn* ?version ?vdate
duplicates drop
collapse (max) wrong* , by(hhid subhh pn asst_type_verif)
reshape wide wrong* , i(hhid subhh pn) j(asst_type_verif) string

lab def yesno 1 "yes" 0 "no"
lab val wrongrecord* yesno
g year = 2004
save asstvrf04.dta, replace


*2006-2014
foreach n in 06 08 10 12 14 {
use assetverif_`n'.dta, clear
if `n' == 06 drop jsubhh
if `n' == 08 drop ksubhh
if `n' == 10 drop lsubhh
if `n' == 12 drop msubhh
if `n' == 14 drop nsubhh

ren ?subhh subhh
ren ?u#* .u#*
ren ?pn_cs pn

unab vars: u*a
local stubs: subinstr local vars "a" "",all
reshape long `stubs',i(hhid pn subhh) j(n) string
duplicates drop
unab vars: u*_7
local stubs: subinstr local vars "7" "",all
di "`stubs'"
reshape long `stubs',i(hhid pn subhh n) j(typasst)
drop if u001 == .
drop n

g str asst_type_verif = ""
replace asst_type_verif = "debts" if typasst == 1
replace asst_type_verif = "trusts" if typasst == 2
replace asst_type_verif = "oth_asst" if typasst == 3
replace asst_type_verif = "transport" if typasst == 4
replace asst_type_verif = "cod" if typasst == 5
replace asst_type_verif = "svng_chck_acc" if typasst == 6
replace asst_type_verif = "bond" if typasst == 7
replace asst_type_verif = "stck" if typasst == 8
replace asst_type_verif = "ira" if typasst == 9
replace asst_type_verif = "bsns" if typasst == 10
replace asst_type_verif = "real_est" if typasst == 11
replace asst_type_verif = "prim_res" if typasst == 12
replace asst_type_verif = "mrtg_prres" if typasst == 13
replace asst_type_verif = "mrtg2_prres" if typasst == 14
replace asst_type_verif = "eqln_prres" if typasst == 15
replace asst_type_verif = "mob_home" if typasst == 16
replace asst_type_verif = "scnd_home" if typasst == 17
replace asst_type_verif = "mrtg_scnd_home" if typasst == 18

g wrongrecord_ = 1 if u001 == 5
replace wrongrecord_ = 0 if u001 == 1

drop typasst u* ?pn* ?version ?vdate
duplicates drop
collapse (max) wrong* , by(hhid  pn asst_type_verif)
reshape wide wrong* , i(hhid pn) j(asst_type_verif) string

lab def yesno 1 "yes" 0 "no"
lab val wrongrecord* yesno
g year = 20`n'
save asstvrf`n'.dta, replace
}

*Append
use asstvrf02.dta, clear
append using asstvrf04.dta asstvrf06.dta asstvrf08.dta asstvrf10.dta asstvrf12.dta asstvrf14.dta
order hhid subhh pn year wrongrecord*
destring hhid subhh pn,replace
save asstvrfpan,replace

*Merge
use sample_capgain,clear
merge m:1 hhid pn year using asstvrfpan, keep(1 3) gen(_asstvrf)

*Erase temporary datasets
foreach n in 02 04 06 08 10 12 14 {
erase assetverif_`n'.dta
erase asstvrf`n'.dta
}
erase sample_capgain.dta
erase asstvrfpan.dta
